package com.vincce.sport.interceptor;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.vincce.sport.entity.SysResource;
import com.vincce.sport.mapper.AdvertMapper;


public class ResourceFilter implements Filter{

	private ApplicationContext ctx;

	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {
		/*ctx = new ClassPathXmlApplicationContext("classpath*:/web-context.xml;classpath*:/application-context.xml;classpath*:/data-context.xml;");
		AdvertMapper advertMapper = (AdvertMapper) ctx.getBean("advertMapper");
		advertMapper.findAllAdvert();
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		HttpServletResponse response = (HttpServletResponse)servletResponse;
		String requestUrl = request.getServletPath();
		boolean flag = false;
		if(requestUrl.indexOf("login.htm") != -1 || requestUrl.indexOf("home.htm") != -1 || requestUrl.indexOf("bkIndex.htm") != -1){
			filterChain.doFilter(servletRequest, servletResponse);
		}else{
			List<SysResource> resourceAllList = (List<SysResource>) request.getSession().getAttribute("resourceAllList");
	    	List<SysResource> resourceRoleList = (List<SysResource>) request.getSession().getAttribute("resourceRoleList");
			if(resourceAllList == null || resourceAllList.isEmpty()){
				response.sendRedirect("/login.htm");
			}*/
			
		/*	//请求是否在资源库中,是则验证改角色是否有权限访问，否则不做拦截
			for(SysResource resource : resourceAllList){
				if(resource.getResourceUrl().indexOf(requestUrl) != -1){
					flag = true;
					break;
				}
			}
			if(flag){
				for(SysResource resource : resourceRoleList){
					if(resource.getResourceUrl().indexOf(requestUrl) != -1){
						flag = true;
						break;
					}
				}
				if(flag){
					filterChain.doFilter(servletRequest, servletResponse);
				}else{
					response.sendRedirect("/login.htm");
				}
			}else{
				filterChain.doFilter(servletRequest, servletResponse);
			}
		}*/
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
